﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_CertifiedHousehold_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Event for control "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Load2Grid()
        End If
    End Sub
    Protected Sub Load2Grid()
        Using _data As New VSEntities
            'load don vi cap giay chung nhan
            Dim _curHuyenId As Integer = Session("HuyenId")
            Dim dv = (From q In _data.DeptCertifieds Where q.ProvinceId = _curHuyenId Order By q.Name Select q.Id, q.Code, q.Name).ToList
            With ddlDonViCap
                .DataSource = dv
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With
            With ddlPhongBanCap
                .DataSource = dv
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With

            'can bo
            Dim cb = (From q In _data.StaffDicts Where q.ProvinceId = _curHuyenId Order By q.Name Select q.Id, q.Code, q.Name).ToList
            With ddlStaffDict
                .DataSource = cb
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With

            'chuc vu
            Dim cv = (From q In _data.PositionDicts Order By q.Name Select q.Id, q.Code, q.Name).ToList
            With ddlPositionDict
                .DataSource = cv
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With
            'phuong xa
            Dim phuongxa = (From q In _data.Provinces Where q.ParentId = _curHuyenId Order By q.Title Select q.Id, q.Code, q.Title).ToList
            With ddlProvince
                .DataSource = phuongxa
                .DataValueField = "Id"
                .DataTextField = "Title"
                .DataBind()
            End With

            'NGanh nghe
            Dim nganhnghe = (From q In _data.DanhMucNganhNghes Where q.CAP5 IsNot Nothing Order By q.Name Select New With {.Id = q.Id, .Name = q.Code & "-" & q.Name}).ToList
            With ddlNganhNgheKinhDoanh
                .DataSource = nganhnghe
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With

            'NGanh nghe
            Dim linhvuc = (From q In _data.BusinessFields Order By q.Name Select New With {.Id = q.Id, .Name = q.Name}).ToList
            With lstBusinessField
                .DataSource = linhvuc
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With

            'dan toc
            Dim dantoc = (From q In _data.Citizens Order By q.Name Select New With {.Id = q.Id, .Name = q.Name}).ToList
            With ddlCitizen
                .DataSource = dantoc
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With

            'quoc gia
            Dim Country = (From q In _data.Countries Order By q.Name Select New With {.Id = q.Id, .Name = q.Name}).ToList
            With ddlCountry
                .DataSource = Country
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With

            'dong vi cap giay chung nhan CMND
            Dim DeptCertified = (From q In _data.DeptIdentifies Order By q.Name Select New With {.Id = q.Id, .Name = q.Name}).ToList
            With ddlDeptCertified
                .DataSource = DeptCertified
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With

            'dong vi cap giay chung nhan CMND

            With ddlDeptCertified2
                .DataSource = DeptCertified
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With
            'giấy tờ khác
            Dim CertifiedPaperDict = (From q In _data.CertifiedPaperDicts Order By q.Name Select New With {.Id = q.Id, .Name = q.Name}).ToList
            With ddlOthersPaper
                .DataSource = CertifiedPaperDict
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
            End With
        End Using
    End Sub
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities


            Dim checkCode = (From q In data.CertifiedHouseholds Where q.CertifiedCode = txtCertifiedCode.Text.Trim() Select q).FirstOrDefault
            If checkCode Is Nothing Then
                Dim p As New VSModel.CertifiedHousehold
                Try

                    'Thông tin chung

                    p.CertifiedCode = txtCertifiedCode.Text.Trim()
                    p.CertifiedDate = StringToDate(txtCertifiedDate.Text.Trim())
                    p.CertifiedStatus = 0 '0, chua duoc duyet

                    'thong tin ca nhan
                    p.Ow_Name = txtOwRepresentative.Text
                    p.Ow_Sex = rdlSex.SelectedValue
                    p.Ow_BirthDate = txtOwDateOfBirth.Text
                    p.Ow_Nationality_ID = ddlCountry.SelectedValue
                    p.Ow_National_ID = ddlCitizen.SelectedValue ' dân toc
                    p.Ow_CardID = txtOwCardId.Text
                    p.Ow_CardDate = StringToDate(txtOwCardDate.Text)
                    p.Ow_Provider_ID = ddlDeptCertified.SelectedValue

                    'giay to khac
                    p.Ow_Doc_ID = ddlOthersPaper.SelectedValue
                    p.Ow_DocCode = txtOtherPaperCode.Text
                    p.Ow_DocDate = StringToDate(txtOtherPaperCode.Text.Trim())
                    p.Ow_DocOffiCertifiedID = ddlDeptCertified2.SelectedValue
                    p.Biz_InvestCapital = txtBiz_InvestCapital.Text.Trim()
                    p.Biz_WordsMoney = txtBiz_WordsMoney.Text.Trim()

                    'Thông tin hộ kinh doanh: 
                    p.Biz_VietName = txtBizName.Text.Trim()
                    p.Ow_Tel = txtOw_Tel.Text.Trim()
                    p.Ow_Fax = txtOw_Fax.Text.Trim()
                    p.Ow_Email = txtOw_Email.Text.Trim()
                    p.Ow_WebSite = txtOw_WebSite.Text.Trim()
                    p.Ow_Resident = txtOw_Resident.Text.Trim()
                    p.XaPhuongId = ddlProvince.SelectedValue

                    'p.Ow_Present = txtOw_Present.Text.Trim()
                    'nganh nghe kinh doanh và linh vưc
                    p.NganhNgheKinhDoanh = ddlNganhNgheKinhDoanh.SelectedValue
                    p.LinhVucKinhDoanh = lstBusinessField.SelectedValue
                    'Mã biên nhận và tên linh vưc
                    p.MaBienNhan = txtCode.Text
                    p.TenLinhVuc = txtName.Text
                    'Thông tin ký giấy chứng nhận: 
                    p.CertifiedProviderOrg = ddlDonViCap.SelectedValue
                    p.CertifiedDeptName = ddlPhongBanCap.SelectedValue 'phong ban cap giay
                    p.ThayMat = txtThayMat.Text
                    p.CertifiedSignFor = txtSignFor.Text
                    p.CertifiedSignerName = ddlStaffDict.SelectedValue
                    p.CertifiedBeHaft = ddlPositionDict.SelectedValue

                    p.Ow_TaxCode = "MST"


                    p.ProvinceId = Session("HuyenId")
                    p.Created = Now
                    p.Creator = Session("UserName")
                    p.StatusId = 1
                    data.CertifiedHouseholds.AddObject(p)
                    data.SaveChanges()

                    Insert_App_Log("Create BusinessField:" & txtName.Text.Trim & "", Function_Name.BusinessField, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Thêm mới dữ liệu thành công.');window.location ='../../Page/CertifiedHousehold/List.aspx?Id=" & Request("Certifiedid") & "';", Me.Page, True)

                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');window.location ='../../Page/CertifiedHousehold/List.aspx';", Me.Page, True)
                End Try
            Else
                Excute_Javascript("Alertbox('Mã đã tồn tại trong hệ thống.')", Me.Page, True)
            End If

        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
